package com.gold.rabbitmqdemo.study1;

import com.gold.rabbitmqdemo.util.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 第一种：简单队列
 * 官网API http://www.rabbitmq.com/tutorials/tutorial-one-java.html
 */
public class SimpleProducer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取连接
        Connection connection= ConnectionUtils.getConnection();
        //创建一个通道channel
        Channel channel=connection.createChannel();
        String queueName="test_simple_queue";

        //声明一个queue： queueDeclare是幂等的操作，只会创建一次，若已存在，则不创建
        channel.queueDeclare(queueName,false,false,false,null);

        //发送消息至队列，message是byte[]
        String message="hello world2";
        channel.basicPublish("",queueName,null,message.getBytes());
        System.out.println("simple send message:"+message);

        //关闭通道channel和连接connection
        channel.close();
        connection.close();
        //发送完成后，可在Web页面的Queue中查看，会有列表记录

    }
}
